package com.example.ymm.modules1.service.impl.sys;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.example.ymm.base.BaseServiceImpl;
import com.example.ymm.modules1.entity.my.sys.DdApproveLog;
import com.example.ymm.modules1.entity.my.sys.DdEventLog;
import com.example.ymm.modules1.entity.my.user.DbUser;
import com.example.ymm.modules1.mapper.my.sys.DdApproveLogMapper;
import com.example.ymm.modules1.query.sys.DdApproveLogQueryCriteria;
import com.example.ymm.modules1.service.dd.DingOaService;
import com.example.ymm.modules1.service.sys.DdApproveLogService;
import com.example.ymm.modules1.service.user.DbUserService;
import com.example.ymm.util.DateKit;
import com.example.ymm.util.QueryHelpPlus;
import com.example.ymm.util.StringUtils;
import com.example.ymm.util.UtilValidate;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.Collections;
import java.util.Date;
import java.util.List;

@Service
@DS("slave2")
@Slf4j
public class DdApproveLogServiceImpl extends BaseServiceImpl<DdApproveLogMapper, DdApproveLog> implements DdApproveLogService {

    @Autowired
    DingOaService dingOaService;
    @Autowired
    DbUserService dbUserService;

    @Override
    public List<DdApproveLog> queryAll(DdApproveLogQueryCriteria criteria) {
        List<DdApproveLog> result =baseMapper.selectList(QueryHelpPlus.getPredicate(DdApproveLog.class, criteria));
        return result;
    }

    @Override
    public void operatorAllDingdingEventLog(DdEventLog ddEventLog, JSONObject json) {
        try {
            /**
             * "result": "agree",
             *     "processInstanceId": "9Qgx5QqjR7axMwxxxx",
             *     "finishTime": 1495592272000,
             *     "createTime": 1495592305000,
             *     "processCode": "Pro-xxx",
             *     "bizCategoryId": "20230xxx",
             *     "businessId": "yewu1",
             *     "type": "finish",
             *     "title": "自测-1016",
             *     "businessType": "yewu1",
             *     "url": "https://aflow.dingtalk.com/dingtalk/mobile/homepage.htm?corpid\\u003dding2c015874d817xxxx\\u0026dd_share\\u003d",
             *     "staffId": "manager75"
             */
            DdApproveLog ddApproveLog = new DdApproveLog();
            ddApproveLog.setResult(json.getString("result"));
            String processInstanceId = json.getString("processInstanceId");
            ddApproveLog.setProcessInstanceId(processInstanceId);
            String processCode = json.getString("processCode");
            ddApproveLog.setProcessCode(processCode);
            ddApproveLog.setTitle(json.getString("title"));
            String typeName=null;
            /**
             * processCode
             * 请假
             * PROC-0EACFF12-E1C1-49DF-95FF-098B055C6BFB
             * 补卡
             * PROC-D8D0BA8E-8736-4D64-B3E1-F6CC1B0BBC7B
             *
             */
            if("PROC-0EACFF12-E1C1-49DF-95FF-098B055C6BFB".equals(processCode)){
                typeName="请假";
            }else if("PROC-D8D0BA8E-8736-4D64-B3E1-F6CC1B0BBC7B".equals(processCode)){
                typeName="补卡";
            }
            ddApproveLog.setTypeName(typeName);

            JSONObject json2 = dingOaService.getProcessDetail(processInstanceId);
            ddApproveLog.setTxtJson(json2.toJSONString());
           // String finishTime = json2.getString("finishTime");
          //  Date f_date = DateKit.dateFormat(finishTime);
          //  String createTime = json2.getString("createTime");
          //  Date c_date = DateKit.dateFormat(createTime);
            String str_time = json2.getString("create_time");
            if(UtilValidate.isNotEmpty(str_time)){
                if(str_time.contains(":")){
                    try {
                        Date date1 = DateKit.dateFormat(json2.getString(("create_time")));
                        Date date2 = DateKit.dateFormat(json2.getString(("finish_time")));
                        if(UtilValidate.isNotEmpty(date1)){
                            ddApproveLog.setCreateTime(new Timestamp(date1.getTime()));
                        }
                        if(UtilValidate.isNotEmpty(date2)){
                            ddApproveLog.setFinishTime(new Timestamp(date2.getTime()));
                        }

                    } catch (Exception e) {
                        log.error(e.getMessage());
                    }

                }else{
                    try {
                        ddApproveLog.setCreateTime(new Timestamp(json2.getInteger(("create_time"))));
                        ddApproveLog.setFinishTime(new Timestamp(json2.getInteger("finish_time")));
                    } catch (Exception e) {
                        log.error(e.getMessage());
                    }

                }
            }

            String user_id = json2.getString("originator_userid");
            ddApproveLog.setUserId(user_id);
            DbUser user = dbUserService.getById(user_id);
            ddApproveLog.setUserName(user.getUserName());
            saveOrUpdate(ddApproveLog);



        }catch (Exception e){
            log.error(e.getMessage(),e);
        }



    }
}
